<?php
# +--------------------------------------------------------
# |Description: 阳光公采接口：地址处理类
# +--------------------------------------------------------
# |Company: 北京申合信科技发展有限公司
# +--------------------------------------------------------
# |WebSite: www.shenhexin.com
# +--------------------------------------------------------
# |Author: Akon<18363851918@163.com>
# +--------------------------------------------------------
# |Date: 2017年6月23日
# +--------------------------------------------------------
namespace Api\Controller;
use Think\Controller;
class AreaController extends Controller{

    /**
     * 获取一级地址
     * URL    http://api.shenhexin.com/api/area/provinces
     * METHOD POST
     * 1、获取access_token，若过期，则重新获取
     * 2、获取一级地址数据
     */
    public function provinces(){
        //$receive = json_decode(file_get_contents('php://input'),true);
        $receive = I('post.');
        A('Common')->checkAccessToken($receive['token']);           //校验access_token是否合法
        $level_1_address = M('Regions','sdb_','DB_CONFIG2')->where('p_region_id is null')->field('region_id id,local_name name')->select();
        A('Common')->errorMsg($level_1_address,'请检查输入是否正确');        //校验返回数据
        $last_level_1_address = array();
        foreach($level_1_address as $key=>$val){                  //重组数组
            $last_level_1_address[$val['name']] = $val['id'];
        }
        $res = array(
            'result'  => $last_level_1_address,
            'success' => 'true',
            'desc'    => ''
        );
        exit(json_encode($res,JSON_UNESCAPED_UNICODE));
    }

    /**
     * 获取二级地址
     * URL    http://api.shenhexin.com/api/area/cities
     * METHOD POST
     * 1、获取access_token,一级地址id
     * 2、校验access_token，若过期，则重新获取
     * 3、获取二级地址数据
     */
    public function cities(){
        //$receive = json_decode(file_get_contents('php://input'),true);
        $receive = I('post.');
        A('Common')->checkAccessToken($receive['token']);          //校验access_token是否合法
        $level_2_address = M('Regions','sdb_','DB_CONFIG2')->where(array('p_region_id'=>$receive['id']))->field('region_id id,local_name name')->select();
        A('Common')->errorMsg($level_2_address,'请检查输入是否正确');
        $last_level_2_address = changeValueToKey($level_2_address,'id','name');
        $res = array(
            'success' => 'true',
            'result'  => $last_level_2_address,
            'desc'    => ''
        );
        exit(json_encode($res,JSON_UNESCAPED_UNICODE));
    }

    /**
     * 获取三级地址
     * URL    http://api.shenhexin.com/api/area/getCounty
     * METHOD POST
     * 1、获取access_token，id
     * 2、校验access_token,若过期，则重新获取
     * 3、获取三级地址数
     */
    public function getCounty(){
        //$receive = json_decode(file_get_contents('php://input'),true);
        $receive = I('post.');
        A('Common')->checkAccessToken($receive['token']);
        A('Common')->errorMsg($receive['id'],'error id');
        $level_3_address = M('Regions','sdb_','DB_CONFIG2')->where(array('p_region_id'=>$receive['id']))->field('region_id id,local_name name')->select();
        A('Common')->errorMsg($level_3_address,'no data');
        $last_level_3_address = changeValueToKey($level_3_address,'id','name');
        $res = array(
            'result'  => $last_level_3_address,
            'success' => 'true',
            'desc'    => ''
        );
        exit(json_encode($res,JSON_UNESCAPED_UNICODE));
    }


}